package com.qiantu.phone.ui.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.LLPwdUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;

public class ChangePwdActivity extends AppActivity implements TextWatcher{
    private String mOldPwd;
    private com.qiantu.phone.widget.EditTextLayout mEditText1;
    private com.qiantu.phone.widget.EditTextLayout mEditText2;
    private android.widget.TextView complete;

    /**
     * 修改密码
     * @param context
     */
    @Log
    public static void startFromReset(Context context, String oldPwd) {
        Intent intent = new Intent(context, ChangePwdActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("oldPwd", oldPwd);
        context.startActivity(intent);
    }

    @Log
    public static void startFromForgetPwd(Context context, String smsId, String phone) {
        Intent intent = new Intent(context, ChangePwdActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("startFromForgetPwd", true);
        intent.putExtra("smsId", smsId);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initView() {
        mEditText1 = findViewById(R.id.edit_text1);
        mEditText1.setLongClickable();
        mEditText2 = findViewById(R.id.edit_text2);
        mEditText2.setLongClickable();
        complete = findViewById(R.id.complete);
        setOnClickListener(complete);
        mEditText1.getEditText().addTextChangedListener(this);
        mEditText2.getEditText().addTextChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == complete){
            if (!mEditText1.getText().toString().equals(mEditText2.getText().toString())){
                toast(R.string.two_msg_not_equal);
                return;
            }
            if (!LLPwdUtil.checkPwd(mEditText1.getText().toString())){
                toast(R.string.pwd_error_hint);
                return;
            }
            if (getBoolean("startFromForgetPwd", false)){
                resetPwd();
            }else {
                updatePwd();
            }
        }
    }

    private void updatePwd(){
        LLHttpManager.updatePwd(getAppActivity(), getString("oldPwd"), mEditText1.getText().toString(), mEditText2.getText().toString(), new HttpCallback<HttpData<Void>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                //AppApplication.getInstance().getLoginInfo().getUserInfo().setHasPassword(true);
                //AppApplication.getInstance().updateLoginInfo();
                LoginActivity.start(getAppActivity(), "", "");
            }
        });


    }

    private void resetPwd(){
        LLHttpManager.resetPwd(getAppActivity(), getString("phone"), getString("smsId"), mEditText1.getText().toString(), mEditText2.getText().toString(), new HttpCallback<HttpData<Void>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                //AppApplication.getInstance().getLoginInfo().getUserInfo().setHasPassword(true);
                //AppApplication.getInstance().updateLoginInfo();
                LoginActivity.start(getAppActivity(), "", "");
            }
        });
    }



    @Override
    protected void initData() {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(mEditText1.getText()) || TextUtils.isEmpty(mEditText2.getText())){
            complete.setEnabled(false);
        }else {
            complete.setEnabled(true);
        }
    }
}